package com.xiaohudie.Lambda;

import org.junit.jupiter.api.Test;

import java.util.Comparator;
import java.util.function.Consumer;

/**
 * @Author xiao hu die
 * @Date 2022/1/16 17:43
 * @Version 1.0
 */
/**lambda是一个实例*/
public class Lambda02 {

    public static void main(String[] args) {

    }
    /**无参无返回值*/
    @Test
    public void test01(){
      Runnable runnable =()-> System.out.println("无参无返回值");
      runnable.run();
    }
    /**有参但是没有返回值
     * 数据类型可以省略
     * */
    @Test
    public void test02(){
        Consumer<String> consumer =(x)-> System.out.println(x);
        consumer.accept("刘碟是一个理想");
    }
    /**lambda只有一个参数，参数的小括号可以省*/
    @Test
    public void test03(){
   Consumer<String> consumer =s -> System.out.println(s);
   consumer.accept("省略小括号");
    }
    /**Lambda需要两个或者以上的参数，多条执行语句，并且有返回值*/
    @Test
    public void test04(){
     /*   Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                System.out.println(o1);
                System.out.println(o2);
                return o1.compareTo(o2);
            }
        };*/
        Comparator<Integer> comparator1 =(x,y)->{
            System.out.println(x);
            System.out.println(y);
            return x.compareTo(y);
        };
        int compare = comparator1.compare(12, 16);
        System.out.println(compare);
    }
    /**当lambda只有一条执行语句的时候,return 与大括号若有，则都可以省略*/
    @Test
    public void test05(){
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        };
        Comparator<Integer> comparator1 =(x,y)->x.compareTo(y);
        int compare = comparator1.compare(10, 2);
        System.out.println(compare);
    }
}
